/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 * */

// AmzServer - Tototoy's server model.
// Author: tonyjobmails@gmail.com (tonyhack).
//
// Define class ByteBufferStatic.
//
// This is a fixed length buffer.
//

#ifndef __BYTE__BUFFER__STATIC
#define __BYTE__BUFFER__STATIC

#include "buffer_define.h"
#include "logging.h"

namespace amz {

// Use stack memory.
template <int BufferSize = kUnitBuffSize>
class ByteBufferStatic {
 public:
  ByteBufferStatic() {}
  ~ByteBufferStatic() {}

  uint8 * BufferPointer() const { return this->buffer_; }

 private:
  uint8 buffer_[BufferSize];
};

// Use heap memory.
class ByteBufferStaticMalloc {
 public:
  explicit ByteBufferStaticMalloc(uint32 buffer_size = kUnitBuffSize) : buffer_(NULL) {
	this->buffer_ = new uint8[buffer_size];
	if(this->buffer_ == NULL) {
	  LOG(FATAL) << "Not enough memory for ByteBufferStatic2";
	}
  }
  ~ByteBufferStaticMalloc() {
    if(this->buffer_) {
	  delete [] this->buffer_;
	  this->buffer_ = NULL;
	}
  }

  inline uint8 * BufferPointer() const { return this->buffer_; }

 private:
  uint8 *buffer_;
};

}  // namespace amz

#endif  // __BYTE__BUFFER__STATIC

